home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / F_emboss.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  5.7 KB  |  190 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <stdio.h>
  19.  
  20. #include <windows.h>
  21. #include <commctrl.h>
  22. #include "resource.h"
  23.  
  24. #include "filter.h"
  25. #include "f_convolute.h"
  26. #include "ScriptInterpreter.h"
  27. #include "ScriptError.h"
  28. #include "ScriptValue.h"
  29.  
  30. extern HINSTANCE g_hInst;
  31.  
  32. struct MyFilterData {
  33.     ConvoluteFilterData cfd;
  34.     LONG height;
  35.     char direction;
  36.     BOOL rounded;
  37. };
  38.  
  39. static int emboss_init(FilterActivation *fa, const FilterFunctions *ff) {
  40.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  41.  
  42.     mfd->direction        = 0;
  43.     mfd->height            = 16;
  44.     mfd->cfd.bias    = 128*256 + 128;
  45.     mfd->cfd.fClip    = TRUE;
  46.  
  47.     mfd->cfd.m[5] = -16;
  48.     mfd->cfd.m[3] = 16;
  49.  
  50.     return 0;
  51. }
  52.  
  53. static BOOL APIENTRY embossDlgProc( HWND hDlg, UINT message, UINT wParam, LONG lParam) {
  54.     switch (message)
  55.     {
  56.         case WM_INITDIALOG:
  57.             {
  58.                 MyFilterData *mfd = (MyFilterData *)lParam;
  59.                 HWND hWnd;
  60.  
  61.                 hWnd = GetDlgItem(hDlg, IDC_HEIGHT);
  62.                 SendMessage(hWnd, TBM_SETTICFREQ, 16, 0);
  63.                 SendMessage(hWnd, TBM_SETRANGE, (WPARAM)TRUE, MAKELONG(1, 256));
  64.                 SendMessage(hWnd, TBM_SETPOS, (WPARAM)TRUE, mfd->height);
  65.                 CheckDlgButton(hDlg, IDC_DIR_MIDDLERIGHT+mfd->direction, TRUE);
  66.  
  67.                 SetWindowLong(hDlg, DWL_USER, (LONG)mfd);
  68.             }
  69.             return (TRUE);
  70.  
  71.         case WM_COMMAND:                      
  72.             if (LOWORD(wParam) == IDOK) {
  73.                 MyFilterData *mfd = (struct MyFilterData *)GetWindowLong(hDlg, DWL_USER);
  74.                 int i;
  75.  
  76.                 mfd->height = SendMessage(GetDlgItem(hDlg, IDC_HEIGHT), TBM_GETPOS, 0, 0);
  77.  
  78.                 for(i=0; i<8; i++)
  79.                     if (IsDlgButtonChecked(hDlg, IDC_DIR_MIDDLERIGHT+i)) {
  80.                         mfd->direction = i;
  81.                         break;
  82.                     }
  83.  
  84.                 mfd->rounded = IsDlgButtonChecked(hDlg, IDC_ROUNDED);
  85.  
  86.                 EndDialog(hDlg, 0);
  87.                 return TRUE;
  88.             } else if (LOWORD(wParam) == IDCANCEL) {
  89.                 EndDialog(hDlg, 1);  
  90.                 return TRUE;
  91.             }
  92.             break;
  93.     }
  94.     return FALSE;
  95. }
  96.  
  97. static const char translate[]={ 5,2,1,0,3,6,7,8 };
  98.  
  99. static int emboss_config(FilterActivation *fa, const FilterFunctions *ff, HWND hWnd) {
  100. //    static char translate[]={ 3,6,7,8,5,2,1,0 };
  101. //    static char translate[]={ 5,8,7,6,3,0,1,2 };
  102.     MyFilterData *mfd;
  103.     int ret;
  104.  
  105.     if (!(mfd = (MyFilterData *)fa->filter_data)) {
  106.         if (!(fa->filter_data = (void *)new MyFilterData)) return 0;
  107.         mfd = (MyFilterData *)fa->filter_data;
  108.  
  109.         memset(mfd, 0, sizeof MyFilterData);
  110.         mfd->height = 16;
  111.     }
  112.  
  113.     ret = DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_FILTER_EMBOSS), hWnd, embossDlgProc, (LONG)fa->filter_data);
  114.  
  115.     memset(mfd->cfd.m, 0, sizeof mfd->cfd.m);
  116.     mfd->cfd.bias    = 128*256 + 128;
  117.     mfd->cfd.fClip    = TRUE;
  118.  
  119.     mfd->cfd.m[translate[mfd->direction]] = -mfd->height;
  120.     mfd->cfd.m[translate[(mfd->direction+4) & 7]] = mfd->height;
  121.     if (mfd->rounded) {
  122.         mfd->cfd.m[translate[(mfd->direction+1) & 7]] =
  123.         mfd->cfd.m[translate[(mfd->direction+7) & 7]] = -(mfd->height+1)/2;
  124.         mfd->cfd.m[translate[(mfd->direction+3) & 7]] =
  125.         mfd->cfd.m[translate[(mfd->direction+5) & 7]] = (mfd->height+1)/2;
  126.     }
  127.  
  128.     return ret;
  129. }
  130.  
  131. static void emboss_string(const FilterActivation *fa, const FilterFunctions *ff, char *buf) {
  132.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  133.  
  134.     sprintf(buf," (%c%c light, height %d)", "MTTTMBBB"[mfd->direction], "RRCLLLCR"[mfd->direction], mfd->height);
  135. }
  136.  
  137. static void emboss_script_config(IScriptInterpreter *isi, void *lpVoid, CScriptValue *argv, int argc) {
  138.     FilterActivation *fa = (FilterActivation *)lpVoid;
  139.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  140.  
  141.     mfd->direction    = argv[0].asInt();
  142.     mfd->height        = argv[1].asInt();
  143.  
  144.     memset(mfd->cfd.m, 0, sizeof mfd->cfd.m);
  145.     mfd->cfd.bias    = 128*256 + 128;
  146.     mfd->cfd.fClip    = TRUE;
  147.  
  148.     mfd->cfd.m[translate[mfd->direction]] = -mfd->height;
  149.     mfd->cfd.m[translate[(mfd->direction+4) & 7]] = mfd->height;
  150.     if (mfd->rounded) {
  151.         mfd->cfd.m[translate[(mfd->direction+1) & 7]] =
  152.         mfd->cfd.m[translate[(mfd->direction+7) & 7]] = -(mfd->height+1)/2;
  153.         mfd->cfd.m[translate[(mfd->direction+3) & 7]] =
  154.         mfd->cfd.m[translate[(mfd->direction+5) & 7]] = (mfd->height+1)/2;
  155.     }
  156. }
  157.  
  158. static ScriptFunctionDef emboss_func_defs[]={
  159.     { (ScriptFunctionPtr)emboss_script_config, "Config", "0ii" },
  160.     { NULL },
  161. };
  162.  
  163. static CScriptObject emboss_obj={
  164.     NULL, emboss_func_defs
  165. };
  166.  
  167. static bool emboss_script_line(FilterActivation *fa, const FilterFunctions *ff, char *buf, int buflen) {
  168.     MyFilterData *mfd = (MyFilterData *)fa->filter_data;
  169.  
  170.     _snprintf(buf, buflen, "Config(%d,%d)", mfd->direction, mfd->height);
  171.  
  172.     return true;
  173. }
  174.  
  175. FilterDefinition filterDef_emboss={
  176.     0,0,NULL,
  177.     "emboss",
  178.     "Converts edges and gradiations in an image to shades, producing a 3D-like emboss effect.\n\n[Assembly optimized] [Dynamic compilation]",
  179.     NULL,NULL,
  180.     sizeof(MyFilterData),
  181.     emboss_init,                NULL,
  182.     filter_convolute_run,
  183.     filter_convolute_param,
  184.     emboss_config,
  185.     emboss_string,
  186.     filter_convolute_start,
  187.     filter_convolute_end,
  188.     &emboss_obj,
  189.     emboss_script_line,
  190. };